﻿using Koala.Pro.CodeGenerator.UI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Koala.Pro.CodeGenerator.UI
{
    public partial class FrmSetColumnAlias : Form
    {
        private bool _isDirty;

        public bool IsDirty
        {
            get
            {
                return _isDirty;
            }
            set
            {
                _isDirty = value;
            }
        }

        public string TableName { get; set; }

        public FrmSetColumnAlias()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                TextBox textBox = control as TextBox;
                if (textBox != null && textBox.Text != textBox.Tag.ToString())
                {
                    _isDirty = true;
                    ColumnNameAliasInfo info = new ColumnNameAliasInfo();
                    info.TableName = TableName;
                    info.ColumnName = textBox.Name;
                    info.ColumnAlias = textBox.Text;
                    info.DatabaseType = Program.global.DbInstance.DatabaseType;
                    info.InsertOrUpdate();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                TextBox textBox = control as TextBox;
                if (textBox != null && textBox.Text != textBox.Tag.ToString())
                {
                    _isDirty = true;
                }
            }
            var message = "您已经对部分字段的别名进行了修改, 你确认放弃所有的修改么?";
            if (_isDirty && DialogResult.No == MessageUtil.ShowYesNoAndTips(message))
            {
                base.DialogResult = DialogResult.None;
            }
        }
    }
}
